package Logger ; 

use strict ; use warnings ; 
use Timer ; 

# the hash holding the vars 
our $confHolder = () ; 

#STOP setting vars 


# =============================================================================
# START OO


# the constructor 
sub new	{
	my $self = shift;
	#get the has containing all the settings
	$confHolder = ${ shift @_ } ; 											
	return bless({}, $self);
} #eof new 


sub AUTOLOAD {
	my $self = shift ; 
	no strict 'refs'; 
    my $name = our $AUTOLOAD;
    *$AUTOLOAD = sub { 
	my $msg = "BOOM! BOOM! BOOM! \n RunTime Error !!!\nUndefined Function $name(@_)\n" ;
	print "$self , $msg";
    };
    goto &$AUTOLOAD;    # Restart the new routine.
}	

sub DESTROY {
	my $self = shift;
	#debug print "the DESTRUCTOR is called  \n" ; 
	return ; 
} 

# STOP OO
# =============================================================================



# =============================================================================
# START functions

# logs a message
sub LogMsg {

	my $self = shift ; 

	# Do not print anything if the LogLevel = 0 
	if ( $confHolder->{'LogLevel'} == 0 ) {	return ; 	}

	# Do not print anything if the PrintMsgs = 0 
	if ( $confHolder->{'PrintMsgs'} == 0 ) 	{	return ; }


	my $objTimer= new Timer(); 
	my $HumanReadableTime = $objTimer->GetHumanReadableTime(); 

	print STDOUT "$HumanReadableTime --- @_ \n" ; 

	if ( $confHolder->{'LogLevel'} >= 4 ) 	{	

		my $LogFile = $self -> GetLogFile();
		$self->AppendToFile( $LogFile , "$HumanReadableTime --- @_ \n" );

	} #eof if


} #eof sub


# logs an error message
sub LogErrorMsg {
	my $self = shift ; 
	my $msg = "@_" ; 
	
# Do not print anything if the PrintErrorMsgs = 0 
if ( $confHolder->{'PrintErrorMsgs'} == 0 ) 	{	return ; }
	
	
	$self->LogMsg("ERROR : $msg " ) ; 
	
} #eof sub

# logs an warning message
sub LogWarningMsg {
	my $self = shift ; 
	my $msg = "@_" ; 

# Do not print anything if the PrintWarningMsgs = 0 
if ( $confHolder->{'PrintWarningMsgs'} == 0 ) 	{	return ; }

	$self->LogMsg("WARNING : $msg " ) ; 
	
} #eof sub


# logs an warning message
sub LogInfoMsg {
	my $self = shift ; 
	my $msg = "@_" ; 
	
	# Do not print anything if the PrintInfoMsgs = 0 
	if ( $confHolder->{'PrintInfoMsgs'} == 0 ) 	{	return ; }

	
	$self->LogMsg("INFO : $msg " ) ; 
	
} #eof sub


# logs a debug message
sub LogDebugMsg {
	my $self = shift ; 
	my $msg = "@_" ; 

	
	# Do not print anything if the PrintDebugMsgs = 0 
	if ( $confHolder->{'PrintDebugMsgs'} == 0 ) 	{	return ; }
	
	$self->LogMsg("DEBUG : $msg "  ) ; 
	
} #eof sub

# logs a debug message
sub LogTraceMsg {

	my $self = shift ; 
	my $msg = "@_" ; 
	my ($package, $filename, $line, $subroutine) = caller(); 	
	
	# Do not print anything if the PrintDebugMsgs = 0 
	if ( $confHolder->{'PrintTraceMsgs'} == 0 ) 	{	return ; }
	
	$self->LogMsg("TRACE : $msg : FROM $package  $filename $line  $subroutine "  ) ; 
	
} #eof sub




sub AppendToFile {

		my $self = shift ; 
		my $FileOutput = shift ; 
		my $StringToPrint = shift ; 
		#READ ALL ROWS OF A FILE TO ALIST 
		open (FILEOUTPUT, ">>$FileOutput") || 
					print "could not open the \$FileOutput $FileOutput!\n"; 
		print  FILEOUTPUT $StringToPrint ; 
		close FILEOUTPUT ;

		#debug $strToReturn .=  $StringToPrint; 

}	#eof sub


sub GetLogFile {

		my $self = shift ; 
		print "The log file is " . $confHolder->{ 'LogFile' } ;  
		my $LogFile = $confHolder->{ 'LogFile' } ; 
		print $confHolder->{ 'LogFile' } ; 
		return $LogFile ; 
		

}	#eof sub



# STOP functions
# =============================================================================


1;

__END__






